﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CERT.Services.DAL;
using CERT.Services;
using System.Web.Mvc;
using GoogleMapsApi.Entities.Geocoding.Request;
using GoogleMapsApi.Engine;
using GoogleMapsApi.Entities.Geocoding.Response;

namespace CERT.Controllers
{
    public class LocationDetailController : ApiController
    {
        private INETServices Service;


        public LocationDetailController()
        {
            Service = new NETServices();

        }

        public LocationDetailController(INETServices service)
        {
            Service = service;

        }

        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
       // [HttpGet("incid/{incid}/location")]
        public Location GetNewLocation(int incidentId)
        {
           //get the incident?
                var loc = new Location();
             //   loc.Incident = incident;
                loc.IncidentId = incidentId;
                loc.StartTime = DateTime.Now;
                loc.EndTime = DateTime.Now.AddHours(1);
                loc.LocationHazards.Add(new LocationHazard());
                return loc;

          

        }

        // GET api/Incident/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public Location GetLocation(int id)
        {
          
                Location incident = Service.GetLocation(a => a.Id == id);
               
                if (incident == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                return incident;
        }

        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public IEnumerable<string> GetLatLong(string address)
        {
            string result = string.Empty;
            var results = new string[1];
            //Instance class use (Geocode)  (Can be made from static/instance class)
            GeocodingRequest geocodeRequest = new GeocodingRequest()
            {
                Address = address,
            };

            GeocodingEngine geocodingEngine = new GeocodingEngine();

            GeocodingResponse geocode = geocodingEngine.GetGeocode(geocodeRequest);

            Console.WriteLine(geocode);
            if (geocode.Results != null)
            {
                GoogleMapsApi.Entities.Geocoding.Response.Result latlong = geocode.Results.FirstOrDefault();
                result = latlong.Geometry.Location.LocationString;
                 results = result.Split(',');
            }

            return results;
        }


        // PUT api/Location/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public HttpResponseMessage PutLocation(int id, Location location)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            try
            {
                Service.UpdateLocation(location, id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

       
        // POST api/Location
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public HttpResponseMessage PostLocation(Location location)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
            try
            {
                Service.AddLocation(location);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            var response = Request.CreateResponse(HttpStatusCode.Created, location);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = location.Id }));
            return response;

        }

        // DELETE api/Incident/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public System.Net.Http.HttpResponseMessage DeleteLocation(int id)
        {

            if (id == 0)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            try
            {
                Service.DeleteLocation(id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK, id);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}